package cn.daoImpl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import cn.dao.CommonDAO;
import cn.utils.PageModel;

public class CommonDaoImpl extends HibernateDaoSupport implements CommonDAO {

	//@Override
	public double Stasistics(String hql) {
		double n=0;
		try {
			n=((Double)this.getSession().createQuery(hql).uniqueResult());
		} catch (Exception e) {
			e.printStackTrace();
			n=0;
		}
		return n;
	}

	//@Override
	public void deleteDemo(Object obj) {

		this.getHibernateTemplate().delete(obj);

	}

	//@Override
	public <T extends Serializable> T findByIntId(int id, T t) {

		return (T)this.getHibernateTemplate().get(t.getClass(), id);
	}

	//@Override
	public PageModel findByPage(String hql, PageModel pm) {
		if(pm.getTotalCount()==0){
			pm.setTotalCount(total(hql));
		}
		if(pm.getTotalCount()>0){//判断是否有记录
			List list=getPageResult(hql, pm);//查询某一页的记录
			if(list.isEmpty()){//判断当前页有没有记录（主要是解决删除最后一页的最后一天记录是跳回前一页）
				pm.setCurrentPage(pm.getCurrentPage()-1);
				list=getPageResult(hql, pm);
			}
			pm.setList(list);
		}else{
			List list = new ArrayList();
			pm.setList(list);
		}
		return pm;
	}

	public List getPageResult(final String hql, final PageModel pm){
		List list=getHibernateTemplate().executeFind(new HibernateCallback() {

			List result=new ArrayList();
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query query=session.createQuery(hql).setMaxResults(pm.getPageSize())
										.setFirstResult(pm.getBeginIndex());
				result=query.list();
				return result;
			}
		});
		return list;
	}

	public int total(String hql){
		int n=0;
		try {
			n=((Long)this.getSession().createQuery("select count(*)  "+hql).uniqueResult()).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			n=0;
		}
		return n;
	}

	//@Override
	public <T extends Serializable> T findByStringId(String id, T t) {
		return (T)this.getHibernateTemplate().get(t.getClass(), id);
	}

	//@Override
	public List findListByHql(String hql, Object[] param) {
		List list=null;
		list=this.getHibernateTemplate().find(hql, param);
		return list;
	}

	//@Override
	public void saveDemo(Object obj) {

		this.getHibernateTemplate().save(obj);

	}

	//@Override
	public void updateDemo(Object obj) {

		this.getHibernateTemplate().merge(obj);
	}

	//@Override
	public boolean updateOrDeleteBysql(final String sql) {
	////采用spring回调hibernate机制实现
		Object obj=getHibernateTemplate().execute(new HibernateCallback() {
			Boolean bon=false;
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {////回调hibernate，获取session
				session.createSQLQuery(sql).executeUpdate();
				bon=true;
				return bon;
			}
		});
		return (Boolean)obj;
	}



	public void deleteBacthDemo(Object[] id,Object objID,Object obj) {
		if(null!=id){
			if (objID instanceof String) {
				for(int i=0;i<id.length;i++){
					String inTemp=(String)id[i];
					this.getHibernateTemplate().delete(this.getHibernateTemplate().get(obj.getClass(), inTemp));
				}
			}
			if (objID instanceof Integer) {
				for(int i=0;i<id.length;i++){
					Integer inTemp=(Integer)id[i];
					this.getHibernateTemplate().delete(this.getHibernateTemplate().get(obj.getClass(), inTemp));
				}
			}
		}
	}

	/**
     * @author gshg
     * @param objs 要删除的实体集合(可以是List，Set Collection)
     */
	public void deleteAll(Collection objs) {

		this.getHibernateTemplate().deleteAll(objs);

	}

	/**
     * @author gshg
     * @param objs 增加或修改的实体集合(可以是List，Set Collection)针对单表操作
     */
	public void saveOrUpdateAll(Collection objs) {

		if(null!=objs&&!objs.isEmpty()) 
		this.getHibernateTemplate().saveOrUpdateAll(objs);

	}


}
